package org.dromara.Nyt7Claim.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.Nyt7Claim.domain.vo.Nyt7ClaimChartVo;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.utils.utils.UserUtil;
import org.springframework.stereotype.Service;
import org.dromara.Nyt7Claim.domain.bo.Nyt7ClaimBo;
import org.dromara.Nyt7Claim.domain.vo.Nyt7ClaimVo;
import org.dromara.Nyt7Claim.domain.Nyt7Claim;
import org.dromara.Nyt7Claim.mapper.Nyt7ClaimMapper;
import org.dromara.Nyt7Claim.service.INyt7ClaimService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 保险理赔Service业务层处理
 *
 * @author yt
 * @date 2024-12-29
 */
@RequiredArgsConstructor
@Service
public class Nyt7ClaimServiceImpl implements INyt7ClaimService {

    private final Nyt7ClaimMapper baseMapper;

    private final UserUtil userUtil;

    /**
     * 查询保险理赔
     *
     * @param id 主键
     * @return 保险理赔
     */
    @Override
    public Nyt7ClaimVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询保险理赔列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保险理赔分页列表
     */
    @Override
    public TableDataInfo<Nyt7ClaimVo> queryPageList(Nyt7ClaimBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Nyt7Claim> lqw = buildQueryWrapper(bo);
        Page<Nyt7ClaimVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的保险理赔列表
     *
     * @param bo 查询条件
     * @return 保险理赔列表
     */
    @Override
    public List<Nyt7ClaimVo> queryList(Nyt7ClaimBo bo) {
        LambdaQueryWrapper<Nyt7Claim> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Nyt7Claim> buildQueryWrapper(Nyt7ClaimBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Nyt7Claim> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getCompanyName() != null, Nyt7Claim::getCompanyName, bo.getCompanyName());
        lqw.eq(StringUtils.isNotBlank(bo.getRegion()), Nyt7Claim::getRegion, bo.getRegion());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceType()), Nyt7Claim::getInsuranceType, bo.getInsuranceType());
        lqw.eq(bo.getClaimArea() != null, Nyt7Claim::getClaimArea, bo.getClaimArea());
        lqw.eq(bo.getClaimHouseholds() != null, Nyt7Claim::getClaimHouseholds, bo.getClaimHouseholds());
        lqw.eq(bo.getClaimAmount() != null, Nyt7Claim::getClaimAmount, bo.getClaimAmount());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            Nyt7Claim::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));

        String postName = userUtil.getPostName();
        if (StringUtils.isNotBlank(postName)){
            lqw.eq(StringUtils.isNotBlank(postName), Nyt7Claim::getCompanyName, postName);
        }

        return lqw;
    }

    /**
     * 新增保险理赔
     *
     * @param bo 保险理赔
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(Nyt7ClaimBo bo) {
        Nyt7Claim add = MapstructUtils.convert(bo, Nyt7Claim.class);

        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改保险理赔
     *
     * @param bo 保险理赔
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(Nyt7ClaimBo bo) {
        Nyt7Claim update = MapstructUtils.convert(bo, Nyt7Claim.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Nyt7Claim entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除保险理赔信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<Nyt7ClaimChartVo> chart(Nyt7ClaimBo bo) {
        LambdaQueryWrapper<Nyt7Claim> lqw = buildQueryWrapper(bo);
        List<Nyt7Claim> nyt7Claims = baseMapper.selectList(lqw);
        if (nyt7Claims.isEmpty()) {
            return Collections.emptyList();
        }

        List<Nyt7ClaimChartVo> chartVos = BeanUtil.copyToList(nyt7Claims, Nyt7ClaimChartVo.class);

        return chartVos.stream().peek(vo -> {

            String originalFormat = "EEE MMM dd HH:mm:ss zzz yyyy";
            String targetFormat = "yyyy-MM-dd";

            SimpleDateFormat originalFormatter = new SimpleDateFormat(originalFormat, Locale.ENGLISH);
            originalFormatter.setTimeZone(TimeZone.getTimeZone("GMT+08:00"));  // 设置时区
            SimpleDateFormat targetFormatter = new SimpleDateFormat(targetFormat);

            try {
                Date date = originalFormatter.parse(vo.getCreateTime());
                String formattedDate = targetFormatter.format(date);
                vo.setCreateTime(formattedDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }).toList();
    }

}
